package com.pronhub.luowowo.website.web.util;

import com.pronhub.luowowo.util.Rediskey;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {
    public static void addcookie(HttpServletResponse response, String key, String value, int time) {
        Cookie cookie = new Cookie(key, value);
        //设置共享路径
        cookie.setPath("/");
        //设置cookie有效时间
        cookie.setMaxAge(time);
        response.addCookie(cookie);
    }

    public static String getToken(HttpServletRequest req, HttpServletResponse response) {
        Cookie[] cookies = req.getCookies();
        String token = "";
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())) {
                    cookie.setMaxAge(Rediskey.USER_LOGIN.getTime().intValue());
                    cookie.setPath("/");
                    response.addCookie(cookie);
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
}
